
UE.formDesignUrl = 'formdesign';
/**
 * 文本框
 * @command textfield
 * @method execCommand
 * @param { String } cmd 命令字符串
 * @example
 * ```javascript
 * editor.execCommand( 'textfield');
 * ```
 */
UE.plugins['text'] = function () {
    var me = this,thePlugins = 'text';
    me.commands[thePlugins] = {
        execCommand:function () {
            var dialog = new UE.ui.Dialog({
                iframeUrl:this.options.UEDITOR_HOME_URL + UE.formDesignUrl+'/text.html',
                name:thePlugins,
                editor:this,
                title: '文本框',
                cssRules:"width:600px;height:310px;",
                buttons:[
                    {
                        className:'edui-okbutton',
                        label:'确定',
                        onclick:function () {
                            dialog.close(true);
                        }
                    },
                    {
                        className:'edui-cancelbutton',
                        label:'取消',
                        onclick:function () {
                            dialog.close(false);
                        }
                    }]
            });
            dialog.render();
            dialog.open();
        }
    };
};

/**
 * 单选框组
 * @command radios
 * @method execCommand
 * @param { String } cmd 命令字符串
 * @example
 * ```javascript
 * editor.execCommand( 'radio');
 * ```
 */
UE.plugins['radios'] = function () {
    var me = this,thePlugins = 'radios';
    me.commands[thePlugins] = {
        execCommand:function () {
            var dialog = new UE.ui.Dialog({
                iframeUrl:this.options.UEDITOR_HOME_URL + UE.formDesignUrl+'/radios.html',
                name:thePlugins,
                editor:this,
                title: '单选框组',
                cssRules:"width:590px;height:370px;",
                buttons:[
                    {
                        className:'edui-okbutton',
                        label:'确定',
                        onclick:function () {
                            dialog.close(true);
                        }
                    },
                    {
                        className:'edui-cancelbutton',
                        label:'取消',
                        onclick:function () {
                            dialog.close(false);
                        }
                    }]
            });
            dialog.render();
            dialog.open();
        }
    };

};
/**
 * 复选框组
 * @command checkboxs
 * @method execCommand
 * @param { String } cmd 命令字符串
 * @example
 * ```javascript
 * editor.execCommand( 'checkboxs');
 * ```
 */
UE.plugins['checkboxs'] = function () {
    var me = this,thePlugins = 'checkboxs';
    me.commands[thePlugins] = {
        execCommand:function () {
            var dialog = new UE.ui.Dialog({
                iframeUrl:this.options.UEDITOR_HOME_URL + UE.formDesignUrl+'/checkboxs.html',
                name:thePlugins,
                editor:this,
                title: '复选框组',
                cssRules:"width:600px;height:400px;",
                buttons:[
                    {
                        className:'edui-okbutton',
                        label:'确定',
                        onclick:function () {
                            dialog.close(true);
                        }
                    },
                    {
                        className:'edui-cancelbutton',
                        label:'取消',
                        onclick:function () {
                            dialog.close(false);
                        }
                    }]
            });
            dialog.render();
            dialog.open();
        }
    };

};
/**
 * 多行文本框
 * @command textarea
 * @method execCommand
 * @param { String } cmd 命令字符串
 * @example
 * ```javascript
 * editor.execCommand( 'textarea');
 * ```
 */
UE.plugins['textarea'] = function () {
    var me = this,thePlugins = 'textarea';
    me.commands[thePlugins] = {
        execCommand:function () {
            var dialog = new UE.ui.Dialog({
                iframeUrl:this.options.UEDITOR_HOME_URL + UE.formDesignUrl+'/textarea.html',
                name:thePlugins,
                editor:this,
                title: '多行文本框',
                cssRules:"width:600px;height:330px;",
                buttons:[
                    {
                        className:'edui-okbutton',
                        label:'确定',
                        onclick:function () {
                            dialog.close(true);
                        }
                    },
                    {
                        className:'edui-cancelbutton',
                        label:'取消',
                        onclick:function () {
                            dialog.close(false);
                        }
                    }]
            });
            dialog.render();
            dialog.open();
        }
    };
};
/**
 * 下拉菜单
 * @command select
 * @method execCommand
 * @param { String } cmd 命令字符串
 * @example
 * ```javascript
 * editor.execCommand( 'select');
 * ```
 */
UE.plugins['select'] = function () {
    var me = this,thePlugins = 'select';
    me.commands[thePlugins] = {
        execCommand:function () {
            var dialog = new UE.ui.Dialog({
                iframeUrl:this.options.UEDITOR_HOME_URL + UE.formDesignUrl+'/select.html',
                name:thePlugins,
                editor:this,
                title: '下拉菜单',
                cssRules:"width:590px;height:370px;",
                buttons:[
                    {
                        className:'edui-okbutton',
                        label:'确定',
                        onclick:function () {
                            dialog.close(true);
                        }
                    },
                    {
                        className:'edui-cancelbutton',
                        label:'取消',
                        onclick:function () {
                            dialog.close(false);
                        }
                    }]
            });
            dialog.render();
            dialog.open();
        }
    };

};




UE.plugins['formdesign'] = function () {
    var me = this,thePlugins = 'formdesign';
    me.commands[thePlugins] = {
        execCommand:function () {
            var dialog = new UE.ui.Dialog({
                iframeUrl:this.options.UEDITOR_HOME_URL + UE.formDesignUrl+'/formDesign.html',
                name:thePlugins,
                editor:this,
                title: '表单设计器 - 清单',
                cssRules:"width:620px;height:220px;",
                buttons:[
                    {
                        className:'edui-okbutton',
                        label:'确定',
                        onclick:function () {
                            dialog.close(true);
                        }
                    }]
            });
            dialog.render();
            dialog.open();
        }
    };
};


UE.registerUI('button_formdesign',function(editor,uiName){

    //注册按钮执行时的command命令，使用命令默认就会带有回退操作
    editor.registerCommand(uiName,{
        execCommand:function(){
            editor.execCommand('formdesign');
        }
    });
    //创建一个button
    var btn = new UE.ui.Button({
        //按钮的名字
        name:uiName,
        //提示
        title:"表单设计器",
        //需要添加的额外样式，指定icon图标，这里默认使用一个重复的icon
        cssRules :'background-position: -401px -40px;',
        //点击时执行的命令
        onclick:function () {
            //这里可以不用执行命令,做你自己的操作也可
            editor.execCommand(uiName);
        }
    });
    return btn;
});
UE.formDesignUtils=(function(){
    var search = window.location.href.split("?")[1];
    if(search){
        search= search.split("#")[0];
    }
    if(!search){
        search = 0;
    }
    return {
        getUrlParam :function (key) {
            if (!search) {
                return;
            }
            var arr = search.split("&");
            for (var i = 0; i < arr.length; i++) {
                var p =  arr[i].split("=");
                if(p.length!=2){
                    continue;
                }
                if(p[0]==key){
                    return p[1];
                }
            }
        },
        getUrlParams:function () {
            return search;
        }
    }
})();

UE.registerUI('button_save',function(editor,uiName){
    //注册按钮执行时的command命令，使用命令默认就会带有回退操作
    editor.registerCommand(uiName,{
        execCommand:function(){
            try {


                if (editor.queryCommandState('source')) {
                    editor.execCommand('source');//切换到编辑模式才提交，否则有bug
                }
                if (editor.hasContents()) {
                    editor.sync();/*同步内容*/
                    //获取表单设计器里的内容
                    var formeditor = editor.getContent();
                    var url = UEDITOR_CONFIG.serverUrl+"?action=activitiSave";
                    var queryStr = UE.formDesignUtils.getUrlParams();
                    if(queryStr){
                        url = url+"&"+queryStr;
                    }
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', url, true);
                    xhr.setRequestHeader('Content-type', 'text/plan');
                    xhr.send(formeditor);
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState == 4 && xhr.status == 200 ) {
                            var result = JSON.parse(xhr.responseText);
                            if(result.state=='SUCCESS'){
                                var dialog = new UE.ui.Dialog({
                                    content:"保存成功",
                                    name:"saveSueecss",
                                    editor:editor,
                                    title: '消息提示',
                                    cssRules:"width:200px;height:100px;",

                                });
                                dialog.render();
                                dialog.open();
                                setTimeout(function () {
                                    dialog.close(true);
                                }, 2000)
                            }else{
                                var dialog = new UE.ui.Dialog({
                                    content:"保存失败"+result.state,
                                    name:"saveSueecss",
                                    editor:editor,
                                    title: '消息提示',
                                    cssRules:"width:200px;height:100px;",

                                });
                                dialog.render();
                                dialog.open();
                                setTimeout(function () {
                                    dialog.close(true);
                                },2000);
                            }

                        }
                    };
                    xhr.onerror= function (ev) { alert("保存错误"+ev) };
                } else {

                    return false;
                }

            } catch ( e ) {
                alert('保存异常');
                console.log(e)
            }

        }
    });
    //创建一个button
    var btn = new UE.ui.Button({
        //按钮的名字
        name:uiName,
        //提示
        title:"保存表单",
        //需要添加的额外样式，指定icon图标，这里默认使用一个重复的icon
        // cssRules :'background-position: -481px -20px;',
        //点击时执行的命令
        onclick:function () {
            //这里可以不用执行命令,做你自己的操作也可
            editor.execCommand(uiName);
        }
    });

    //因为你是添加button,所以需要返回这个button
    return btn;
});


